package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.Vehicle;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.VehicleMapper;
import com.ruoyi.system.service.IVehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 车辆信息Service业务层处理
 *
 * @author ruoyi
 */
@Service
public class VehicleServiceImpl implements IVehicleService {
    @Autowired
    private VehicleMapper vehicleMapper;

    /**
     * 查询车辆信息
     *
     * @param vehicleId 车辆ID
     * @return 车辆信息
     */
    @Override
    public Vehicle selectVehicleById(Long vehicleId) {
        return vehicleMapper.selectVehicleById(vehicleId);
    }

    /**
     * 查询车辆信息列表
     *
     * @param vehicle 车辆信息
     * @return 车辆信息集合
     */
    @Override
    public List<Vehicle> selectVehicleList(Vehicle vehicle) {
        return vehicleMapper.selectVehicleList(vehicle);
    }

    /**
     * 查询车辆信息列表
     *
     * @param vehicle 车辆信息
     * @return 车辆信息集合
     */
    @Override
    public List<Vehicle> selectVehicleByPhone(Vehicle vehicle) {
        return vehicleMapper.selectVehicleByPhone(vehicle);
    }

    /**
     * 新增车辆信息
     *
     * @param vehicle 车辆信息
     * @return 结果
     */
    @Override
    public int insertVehicle(Vehicle vehicle) {
        vehicle.setCreateTime(DateUtils.getNowDate());
        return vehicleMapper.insertVehicle(vehicle);
    }

    /**
     * 修改车辆信息
     *
     * @param vehicle 车辆信息
     * @return 结果
     */
    @Override
    public int updateVehicle(Vehicle vehicle) {
        vehicle.setUpdateTime(DateUtils.getNowDate());
        return vehicleMapper.updateVehicle(vehicle);
    }

    /**
     * 批量删除车辆信息
     *
     * @param vehicleIds 需要删除的车辆ID
     * @return 结果
     */
    @Override
    public int deleteVehicleByIds(Long[] vehicleIds) {
        return vehicleMapper.deleteVehicleByIds(vehicleIds);
    }

    /**
     * 删除车辆信息
     *
     * @param vehicleId 车辆ID
     * @return 结果
     */
    @Override
    public int deleteVehicleById(Long vehicleId) {
        return vehicleMapper.deleteVehicleById(vehicleId);
    }


    @Override
    public boolean checkLicensePlateUnique(Vehicle vehicle) {
        Long vehicleId = StringUtils.isNull(vehicle.getId()) ? -1L : vehicle.getId();
        Vehicle info = vehicleMapper.checkLicensePlateUnique(vehicle.getLicensePlate());
        Boolean isExist = StringUtils.isNull(info) || info.getId().equals(vehicleId);
        return isExist;
    }
}
